//-----	Main.c							---------//
//-----	Aurthur: Chris Chester					---------//
//-----	Date: 29/07/2011					---------//
//-----	Capacitive Sensing on PIC16 chip with a capacitive mat.	---------//


#include <htc.h>
#include <stdio.h>
#include <stdlib.h>

__CONFIG(FOSC_INTOSC & WDTE_OFF);

#define FREQTHRESHOLD 290     // 100 for mylar dielec, 290 for napkin

void interrupt ISR();
void initialise();

void display(int);

int count = 0;
int test = 0;
int time[30];


/*Main function*/
/*==================================================================================================================================*/	
void main(void){

	initialise();
	
	while(1)
	{
		continue;
	}	
}
/*==================================================================================================================================*/	




/* Interupt for Pressure Pad*/
/*==================================================================================================================================*/
void interrupt ISR()
{
	
		
	//If timer0 overflow
	//The overflow flag should stop timer1 from counting as it will change the gate bit.
	if(TMR0IE && TMR0IF)
	{
		count++;
		
		if (count>200)
		{
			time[test] = TMR1;
			
				if( TMR1 < FREQTHRESHOLD) 
				{
					RA7 = 1;		// Turns on LED
					_delay(100000); 	// Waits one seconds
				}
				else
				{
					RA7 = 0;		// Turns off LED
				}			
		
		
			TMR0 = 0xFF;			// Sets timer to highest position in order to reset the gate on the capcitor sensor. 
			TMR0IF = 0;			// Clears Flag
			TMR0 = 0x00;	
			
		
			// Resets the Timer0
			TMR1ON = 0; // Stop Timer1
			TMR1L = 0x00; // Reset Timer1
			TMR1H = 0x00;
			TMR1 = 0x0000;
			TMR1ON = 1; // Restart Timer1
			count = 0;

			if (test> 30)
			{
				return;
			}
			
			if ( time[test] > 0)
			{
				test++;
			}
		}
		
	//	TMR0 = 0xFF;			// Sets timer to highest position in order to reset the gate on the capcitor sensor. 
	//	TMR0IF = 0;			// Clears Flag
	//	TMR0 = 0x00;	
			
			
	}
}
/*==================================================================================================================================*/

/* Initialising Function*/
/* Uses code from PCBheaven.com*/
/*==================================================================================================================================*/
void initialise(void)
{
		TRISB = 0b00000010; 	//Sets RB1 as an input 
		TRISA = 0b00000000; 	//Sets all portA as outputs
		PORTA = 0x00;		//Clears all portA outputs
		PORTB = 0x00;		//Clears all PortB outputs
		ANSELB |= ANSB1;	//Sets RB1 as an analogue input
		
		CPSCON0 = 0b10001100;	//Enables Capacitive Sensing
		CPSCON1 = 0b00000000;	//Clears Sensing Choice
	
		
		OPTION_REG = 0b11000111;//-- Using the capacitance module with TMR0 as the reference time--//
					   
					/*
						Bit 5: TMR0 Source is internal instruction clock (Fosc/4)
						Bit 4: TRM0 Increments on low to high
						Bit 3: Prescaller assigned to TMR0 module
						Bits <2:0>: Prescaller rate is 1:256
						This gives a clock of 32M/4/64 = 125kHz
					*/
					   				
		TMR0IF = 0; 		// Clears Timer0 Interrupt Flag
		TMR0IE = 1; 		// Enables Timer0 overflow interupt
	
		T1CON = 0b11000101;  	//--Setup TMR1 module Control register--//
		
					/*	Bits <7:6>: Timer 1 source is Capacitive Sensing Oscillator
					 	Bits <5:4>: Prescaller is 1:1
						Bit 3: LP oscillatos is deactivated
						Bit 2: No synchronization
						Bit 0: Timer 1 is on
					*/
	
		T1GCON = 0b11100001;  	//--Setup Timer 1 Gate module--//
		
					/*
						Bit 7: Gate controls the counting function of TMR1 (want 1)
						Bit 6: Gate polarity is Active HIGH
						Bit 5: Gate input is through Toggle FF
						Bits <4:3>: No single pulse mode
						Bits <1:0>: Gate source is Timer0 overflow
					*/
		
		TMR1GIF = 0;		//Clear Gate interupt
		TMR1GIE = 1;		//Enable Gate interupt
		
		CPSCON1 |= 0b00001011;	// Used to set capacitive sensing pin RB1 (CPS11)
				
		ei();			//Turn on Global Interrupts
}
/*==================================================================================================================================*/	